package com.sdy.healthcode.biz.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.sdy.healthcode.biz.config.SmsConfig;
import com.sdy.healthcode.biz.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SmsServiceImpl implements SmsService {
    @Autowired
    private SmsConfig smsConfig;
    
    @Override
    public void sendSmsCode(String phone, String smsCode) {
        log.info("发送短信验证码，手机号：{}，内容：{}", smsCode, phone);
//        send(phone, smsCode);
    }

    private void send(String phone, String smsCode) {
        DefaultProfile profile = DefaultProfile.getProfile(smsConfig.getRegionId(), smsConfig.getAccessKeyId(), smsConfig.getAccessSecretId());
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", smsConfig.getRegionId());
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "123414");
        request.putQueryParameter("TemplateCode", "tmplate");
        request.putQueryParameter("TemplateParam", "{\"smsCode\":\"#SMS_CODE#\"}".replace("#SMS_CODE#", smsCode));
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("短信接口返回：" + response.getData());
        } catch (ClientException e) {
            log.error("", e);
            throw new RuntimeException(e);
        }
    }
}
